Page({
  data: {
      deliveryType: 'delivery', // 默认配送
      showDatePicker: false,
      showTimePicker: false,
      dateOptions: [],
      selectedDate: '', // 用于显示的文字（今天 8月5日）
      displayTime: '',
      timeValue: '',
      couponValue: 0,
      selfPickupAnimation: {}
  },

  // 页面加载
  onLoad() {
      this.initDateOptions();
      const now = new Date();
      const rounded = this.getNextHalfHour(now);
      this.setData({
          timeValue: this.formatTimeForPicker(rounded)
      });

      this.initAnimations();
  },

  // 初始化未来7天的日期选项
 initDateOptions() {
     const options = [];
     const today = new Date();

     for (let i = 0; i < 7; i++) {
         const date = new Date(today);
         date.setDate(today.getDate() + i);
         let label = '';
         if (i === 0) {
             label = '今天';
         } else if (i === 1) {
             label = '明天';
         } else if (i === 2) {
             label = '后天';
         } else {
             const week = ['日', '一', '二', '三', '四', '五', '六'];
             label = `周${week[date.getDay()]}`;
         }

         const month = date.getMonth() + 1;
         const day = date.getDate();
         const labelText = `${label} ${month}月${day}日`;

         // 存 label 和实际值
         options.push({
             label: labelText,
             value: `${month}-${day}`
         });
     }

     // 把 label 列表单独提取给 picker 用
     const labels = options.map(item => item.label);

     this.setData({
         dateOptions: options,
         dateLabels: labels // 新增
     });
 },
 goToRemarks(){
    wx.navigateTo({
      url: '/wqfGroupBuying/pages/wqfGroupBuyingRemarks/wqfGroupBuyingRemarks',
    })
 },

  // 打开日期选择
  // onSelectTime() {
  //     this.setData({
  //         showDatePicker: true
  //     });
  // },

  // 关闭日期选择
  // onCloseDatePicker() {
  //     this.setData({
  //         showDatePicker: false
  //     });
  // },

  // 确认日期选择
// onConfirmDate(event) {
//   const index = event.detail.index;
//   const selected = this.data.dateOptions[index];

//   this.setData({
//       showDatePicker: false,
//       selectedDate: selected.label // 展示用
//   }, () => {
//       this.setData({
//           showTimePicker: true
//       });
//   });
// },

  // 关闭时间选择
  // onCloseTimePicker() {
  //     this.setData({
  //         showTimePicker: false
  //     });
  // },

  // 确认时间选择
  // onConfirmTime(event) {
  //     // event.detail 是字符串或 Date，根据组件决定
  //     let timeStr = '';

  //     if (typeof event.detail === 'string') {
  //         timeStr = event.detail;
  //     } else if (event.detail instanceof Date) {
  //         timeStr = this.formatTimeForPicker(event.detail);
  //     } else if (event.detail.value) {
  //         timeStr = event.detail.value;
  //     }

  //     this.setData({
  //         showTimePicker: false,
  //         timeValue: timeStr,
  //         displayTime: `${this.data.selectedDate} ${timeStr}`
  //     });
  // },

  // 格式化为 "HH:mm"
  formatTimeForPicker(date) {
      const hours = date.getHours().toString().padStart(2, '0');
      const minutes = date.getMinutes().toString().padStart(2, '0');
      return `${hours}:${minutes}`;
  },

  // 向上取整到下一个半小时
  getNextHalfHour(date) {
      const d = new Date(date);
      const minutes = d.getMinutes();
      if (minutes < 30) {
          d.setMinutes(30);
      } else {
          d.setHours(d.getHours() + 1);
          d.setMinutes(0);
      }
      d.setSeconds(0);
      d.setMilliseconds(0);
      return d;
  },

  // 其他不变
  initAnimations() {
      this.selfPickupAni = wx.createAnimation({
          duration: 300,
          timingFunction: 'ease-in-out'
      });
  },

  onDeliveryChange(event) {
      const isDelivery = event.detail;
      const deliveryType = isDelivery ? 'delivery' : 'self';

      if (deliveryType === 'self') {
          this.selfPickupAni.height(0).opacity(0).step();
          this.setData({
              selfPickupAnimation: this.selfPickupAni.export()
          });

          setTimeout(() => {
              this.setData({
                  deliveryType
              });
              setTimeout(() => {
                  this.selfPickupAni.height('auto').opacity(1).step();
                  this.setData({
                      selfPickupAnimation: this.selfPickupAni.export()
                  });
              }, 50);
          }, 300);
      } else {
          this.selfPickupAni.height(0).opacity(0).step();
          this.setData({
              selfPickupAnimation: this.selfPickupAni.export(),
              deliveryType
          });
      }
  },

  onSelectCoupon() {
      wx.showToast({
          title: '选择优惠券',
          icon: 'none'
      });
  },

  onClickButton() {
      wx.showToast({
          title: '提交订单',
          icon: 'none'
      });
  }
});
